home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / scsh-0.4 / scsh-0 / scsh-0.4.2 / scsh / hpux / stdio_dep.c < prev    next >
C/C++ Source or Header  |  1995-10-25  |  2KB  |  85 lines

  1. /* Copyright (c) 1994 by Olin Shivers.
  2. ** Copyright (c) 1994-1995 by Brian D. Carlstrom.
  3. **
  4. ** This file implements the char-ready? procedure for file descriptors
  5. ** and Scsh's fdports. It is not Posix, so it must be implemented for
  6. ** each OS to which scsh is ported.
  7. **
  8. ** This version assumes two things:
  9. ** - the existence of select to tell if there is data
  10. **   available for the file descriptor.
  11. ** - the existence of the _cnt field in the stdio FILE struct, telling
  12. **   if there is any buffered input in the struct.
  13. **
  14. ** Most Unixes have these things, so this file should work for them.
  15. ** However, Your Mileage May Vary.
  16. **
  17. ** You could also replace the select() with a iotctl(FIONREAD) call, if you
  18. ** had one but not the other.
  19. **     -Olin&Brian
  20. */
  21.  
  22. #include <sys/types.h>
  23. #include <sys/time.h>
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include "libcig.h"
  27. #include <errno.h>
  28.  
  29. #include "stdio_dep.h"    /* Make sure the .h interface agrees with the code. */
  30.  
  31. /* These two procs return #t if data ready, #f data not ready, 
  32. ** and errno if error.
  33. */
  34.  
  35. scheme_value char_ready_fdes(int fd)
  36. {
  37.   fd_set readfds;
  38.   struct timeval timeout;
  39.   int result;
  40.  
  41.   FD_ZERO(&readfds);
  42.   FD_SET(fd,&readfds);
  43.  
  44.   timeout.tv_sec=0;
  45.   timeout.tv_usec=0;
  46.  
  47.   result=select(fd+1, &readfds, NULL, NULL, &timeout);
  48.   
  49.   if(result == -1 ) 
  50.     return(ENTER_FIXNUM(errno));
  51.   if(result) 
  52.     return(SCHTRUE);
  53.   return(SCHFALSE);    
  54. }
  55.  
  56. scheme_value stream_char_readyp(FILE *f)
  57. {
  58.   int fd = fileno(f);
  59.   return f->_cnt > 0 ? SCHTRUE : char_ready_fdes(fd);
  60. }
  61.  
  62. void setfileno(FILE *fs, int fd)
  63. {
  64.   fs->__fileL = (fd & 0xFF);
  65.   fs->__fileH = ((fd>>8) & 0xFF);
  66. }
  67.  
  68. int fbufcount(FILE* fs)
  69.   return(fs->_cnt);
  70. }
  71.  
  72. /* Returns true if there is no buffered data in stream FS
  73. ** (or there is no buffering, period.)
  74. */
  75.  
  76. int ibuf_empty(FILE *fs) {return fs->_cnt <= 0;}
  77.  
  78.  
  79. /* Returns true if the buffer in stream FS is full 
  80. ** (or there is no buffering, period).
  81. */
  82.  
  83. int obuf_full(FILE *fs) {return fs->_cnt <= 0;}
  84.